﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Net;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.ComponentModel;



public partial class FlickrRSS : System.Web.UI.UserControl
{
    #region Internal Class to store details of images in list
    /// <summary>
    /// Internal Class to Represent an Image in the Galler
    /// </summary>
    public class GalleryImage
    {

        protected string _ImageUrl;
        protected string _ThumbnailUrl;
        protected string _title;
        

        public string ImageUrl
        {
            get
            {
                return _ImageUrl;
            }
        }

        public string ThumbnailUrl
        {
            get
            {
                return _ThumbnailUrl;
            }
        }

        public string Title
        {
            get
            {
                return _title;
            }
        }


        public GalleryImage(string imageUrl, string thumbnailUrl, string title)
        {
            _ImageUrl = imageUrl;
            _ThumbnailUrl = thumbnailUrl;
            _title = title;
        }
    }
    #endregion


    public enum FlickrRSSType
    {
        user, group, allpublic
    }

    private FlickrRSSType _type = FlickrRSSType.allpublic;
    private string _userid = "";
    private string _tag = "";
    private int _maxNoOfImages = 10;
    private int _cacheDuration = 60;

    [DefaultValue(10),Browsable(true)]
    [Category("Appearance")]
    [DescriptionAttribute("How many photos you want to appear")] 
    public int NumberOfImages
    {
        get { return _maxNoOfImages; }
        set { _maxNoOfImages = value; }
    }

    [Category("Appearance")]
    public string Tags
    {
        get { return _tag; }
        set { _tag=value;}
    }

    [Category("Appearance")]
    public string FlickrId
    {
        get { return _userid; }
        set { _userid = value; }
    }

    /// <summary>
    /// Number of minuts between the RSS document is read from Flicker
    /// </summary>
    [DefaultValue(60)]
    [Browsable(true)]
    [Category("Appearance")]
    [DescriptionAttribute("Number of minuts between the RSS document is read from Flicker")] 
    public int CacheDuration
    {
        get { return _cacheDuration; }
        set { _cacheDuration = value; }
    }

    [DefaultValue(FlickrRSSType.allpublic)]
    [Browsable(true)]
    [Category("Appearance")]
    public FlickrRSSType RssType
    {
        get { return _type; }
        set { _type = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        string cacheKey = ClientID;

        List<GalleryImage> images = HttpRuntime.Cache[cacheKey] as List<GalleryImage>; 

        if(images==null)
        {
            string _rssUrl = "";

            switch (_type)
            {
                case FlickrRSSType.allpublic:
                    _rssUrl = "http://api.flickr.com/services/feeds/photos_public.gne?tags={0}&format=rss_200";
                    break;
                case FlickrRSSType.user:
                    _rssUrl = "http://api.flickr.com/services/feeds/photos_public.gne?id={1}&tags={0}&format=rss_200";
                    break;
                case FlickrRSSType.group:
                    _rssUrl = "http://api.flickr.com/services/feeds/groups_pool.gne?id={1}&format=rss_200;";
                    break;
                default:
                    break;
            }

            _rssUrl = string.Format(_rssUrl, _tag, _userid);

            WebRequest request = WebRequest.Create(_rssUrl);
            try
            {
                using (WebResponse response = request.GetResponse())
                {
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(response.GetResponseStream());

                    XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmlDoc.NameTable);
                    nsMgr.AddNamespace("media", "http://search.yahoo.com/mrss/");

                    XmlNodeList nodes = xmlDoc.SelectNodes("rss/channel/item");

                    images = new List<GalleryImage>();

                    int ImagesLoaded = 0;
                    foreach (XmlNode node in nodes)
                    {
                        if (ImagesLoaded >= _maxNoOfImages) break;

                        string url = node.SelectSingleNode("link").InnerText;
                        string thumbnail = node.SelectSingleNode("media:thumbnail", nsMgr).Attributes.GetNamedItem("url").Value;
                        string title = node.SelectSingleNode("title").InnerText;
                        
                        images.Add(new GalleryImage(url, thumbnail, title));
                        ImagesLoaded++;
                    }

                    if (_cacheDuration > 0)
                    {
                        DateTime _expiration = DateTime.Now.AddMinutes(_cacheDuration);
                        Cache.Insert(cacheKey, images, null, _expiration, TimeSpan.Zero);
                    }
                }
            }
            catch
            { }
        }

        rpImages.DataSource = images;
        rpImages.DataBind();
    }
}
